package com.sinping.qh.api.admin.common;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.admin.DocumentDto;
import com.sinping.qh.service.admin.IDocumentService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(value = "admin文档管理",description = "admin文档管理API")
@RequestMapping(value = Const.ADMIN_URI +"document",name = "文档管理")
public class DocumentEndPoint extends BaseAppEndPoint {

    @Autowired
    private IDocumentService iDocumentService;

    @ApiOperation(value = "添加文档")
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResDto addDocument(HttpServletRequest httpServletRequest,@RequestParam(required = true) @ApiParam(name = "msg",value = "{\"file_name\":\"文档一\",\"file_type\":\"文档类型\",\"file_notice_no\":\"\",\"file_path\":\"http://www.baidu.com/a.pdf\"}") String msg){

        DocumentDto documentDto = JsonMapper.defaultMapper().fromJson(msg,DocumentDto.class);

        checkNull(documentDto, ConstEum.PARAM_ERROR);

        ResDto resDto = iDocumentService.addDocument(httpServletRequest,documentDto);

        return resDto;
    }

    @ApiOperation("删除文档")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResDto deleteDocumentById(@ApiParam(name = "id",value = "123") @PathVariable String id){
        ResDto resDto = iDocumentService.deleteDocumentById(id);
        return resDto;
    }

    @ApiOperation("获取文档详情")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResDto getDocumentById(@ApiParam(name = "id",value = "123") @PathVariable String id){
        ResDto resDto = iDocumentService.getDocumentById(id);
        return resDto;
    }

    @ApiOperation("分页查询文档")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResDto getMessageInfoFrontPage(@ApiParam(name = "msg",value = "{\"page_size\":10, \"page_current\":1,\"condition\":{\"file_name\":\"123\",\"file_type\":\"\"}}") @RequestParam(required = true) String msg){
        FrontPage frontPage = JsonMapper.defaultMapper().fromJson(msg, FrontPage.class);
        checkNull(frontPage,ConstEum.PARAM_ERROR);
        ResDto resDto = iDocumentService.getDocumentListFrontPage(frontPage);
        return resDto;
    }
}
